package vn.conghuy.talkwithme.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import vn.conghuy.talkwithme.data.android.ContactModel;

public class Parser {
	
	private static final String NEXT_KEYWORD = "next";
	private static final String DAY_KEYWORD = "day";
	private static final String DAYS_KEYWORD = "days";
	private static final String HOUR_KEYWORD = "hour";
	private static final String HOURS_KEYWORD = "hours";
	private static final String TOMORROW_KEYWORD = "tomorrow";
	private static final String TODAY_KEYWORD = "today";
	private static final String AT_KEYWORD = "at";
	
	public static final int PARSE_ADDRESS_OK = 0;			//return when parse 1 address 
	public static final int PARSE_ADDRESS_FAIL = 1;			//return when parse address fail
	public static final int PARSE_ADDRESS_MULTI = 2;		//return when parse multi address
	
	public static boolean parseDate(String datePhrase, DateWrapper dateWrapper){
		String[] words = datePhrase.split(" ");
		
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(System.currentTimeMillis());
		
		//next 3 days
		IntWrapper numWrapper = new IntWrapper();
		if(words[0].equals(NEXT_KEYWORD) && Utils.isNumber(words[1], numWrapper)){
			int num = numWrapper.getInt();
			String word2 = words[2];
			if(word2.equals(DAY_KEYWORD) || word2.equals(DAYS_KEYWORD)){
				cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) + num);
				dateWrapper.setDate(cal.getTime());
				return true;
			}
			else if(word2.equals(HOUR_KEYWORD) || word2.equals(HOURS_KEYWORD)){
				cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY) + num);
				dateWrapper.setDate(cal.getTime());
				return true;
			}
			return false;
		}
		else if(words[0].equals(TOMORROW_KEYWORD)){
			cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) + 1);
			dateWrapper.setDate(cal.getTime());
			return true;
		}
		else if(words[0].equals(TODAY_KEYWORD)){
			dateWrapper.setDate(cal.getTime());
			return true;
		}
		else{
			DateFormat formater = new SimpleDateFormat("MMM dd yyyy", Locale.US);
			try {
				dateWrapper.setDate(formater.parse(datePhrase));
				return true;
			} catch (ParseException e) {
			}
			return false;
		}
	}
	
	public static int parseAddressFromNamePhrase(String namePhrase, ArrayList<ContactModel> listContact, ArrayListContactWrapper lCallContactWrapper, StringWrapper addressWrapper){
		
		// check strName is exist in contacts, if it is a contact, get number.
		lCallContactWrapper.setArrayList(ContactModel.isExistWordInContactEqualWordInString(listContact, namePhrase));
		if( lCallContactWrapper.getArrayList().size() > 0){
			if(lCallContactWrapper.getArrayList().size() == 1){						// has 1 contact match
				addressWrapper.setStr(lCallContactWrapper.getArrayList().get(0).getAddress());
				return PARSE_ADDRESS_OK;
			}
			else{												//has more than 1 contact match
				ContactModel contactMatch = ContactModel.isExistElemInArrayHasNameBeSubStringOfStr(listContact, namePhrase);
				if(contactMatch != null){
					//addressWrapper.setStr(contactMatch.getAddress());
					lCallContactWrapper.arrayList.add(contactMatch);
					//return PARSE_ADDRESS_OK;
				}
				return PARSE_ADDRESS_MULTI;
			}
		}
		//else check strName exist number.
		else{
			addressWrapper.setStr(Utils.numberFromString(namePhrase));
			if(addressWrapper.getStr().equals("") == false)
				return PARSE_ADDRESS_OK;
			else return PARSE_ADDRESS_FAIL;
		}
	}
	
	public static boolean parseEmail(String addressPhrase, StringWrapper outAddress){
		
		addressPhrase = addressPhrase.trim();
		
		Pattern p = Pattern.compile(".+@.+\\.[a-z]+");
		Matcher m;
		boolean isMatch;
		
		// It not match normal email. Check address is wrote with natural language
		// Ex : steven gerrard at gmail dot com, steven gerrard at gmail.com
		int atIndex = Utils.indexOfWord(addressPhrase, AT_KEYWORD);
		if(atIndex != -1){
			String usernamePhrase = addressPhrase.substring(0, atIndex - 1);
			String domainPhrase = addressPhrase.substring(atIndex + AT_KEYWORD.length() + 1);
			
			String username = usernamePhrase.replace(" ", "");
			String domain = domainPhrase.replace(" dot ", ".");
			String email = username + "@" + domain;
			outAddress.setStr(username + "@" + domain);
			
			m = p.matcher(email);
			isMatch = m.matches();
			if(isMatch== true){
				if(outAddress != null) outAddress.setStr(email);
				return true;
			}
		}
		
		// Check is normal email address. Ex : conghuy@gmail.com
		m = p.matcher(addressPhrase);
		isMatch = m.matches();
		if(isMatch== true){
			if(outAddress != null) outAddress.setStr(addressPhrase);
			return true;
		}
		
		return false;
	}
}
